package com.example.graduation.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j(topic = "RedisUtil")
@SuppressWarnings("all")
public class RedisUtil {
    /*
  默认秒数 30s
   */
    private static final Long DEFAULT_EXPIRE_TIME_SECONDS = 30L;

    /*
    默认分钟 5min
     */
    private static final Long DEFAULT_EXPIRE_TIME_MINUTE = 5L;

    /*
    默认小时 1h
     */
    private static final Long DEFAULT_EXPIRE_TIME_HOUR = 1L;

    /*
    默认天数 1d
     */
    private static final Long DEFAULT_EXPIRE_TIME_DAY = 3L;

    /*
    默认月数 1
     */
    private static final Long DEFAULT_EXPIRE_TIME_MOUTH = 1L;

    /*
     缓存当前用户每天发布的职位条数
     */
    public static String CREATE_JOB_TODAY = "createJobToday";


    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 存入key-value
     *
     * @param key   key
     * @param value value
     * @return boolean
     */
    public boolean set(final String key, Object value) {
        boolean flag = false;
        try {
            redisTemplate.opsForValue().set(key, value);
            flag = true;
            log.info("存入键值对 " + key + " - " + value + "  成功");
        } catch (Exception e) {
            log.info("set操作发生异常 : {}", e.getMessage());
        }
        return flag;
    }

    /**
     * 存入key-value 带自动消失时间
     *
     * @param key        key
     * @param value      value
     * @param expireTime 自动消失时间
     * @param timeUnit   单位
     * @return boolean
     */
    public boolean set(final String key, Object value, Long expireTime, TimeUnit timeUnit) {
        boolean flag = false;
        try {
            redisTemplate.opsForValue().set(key, value, expireTime, timeUnit);
            flag = true;
            log.info("存入键值对 " + key + " - " + value + "  成功");
        } catch (Exception e) {
            log.error("set操作发生异常 : {}", e.getMessage());
        }
        log.info("存入结果 : {}", flag);
        return flag;
    }

    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            log.info("删除 key : {}", key);
            remove(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param pattern
     */
    public void removePattern(final String pattern) {
        Set<String> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0) {
            redisTemplate.delete(keys);
        }
    }

    /**
     * 删除对应的value
     *
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
            log.info("删除了key : {}", key);
        }
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        Boolean flag = redisTemplate.hasKey(key);
        log.info(key + "是否存在? : {}", flag);
        return flag;
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public Object get(final String key) {
        Object result = null;
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }

    /**
     * 哈希 添加
     *
     * @param key
     * @param hashKey
     * @param value
     */
    public void hmSet(String key, Object hashKey, Object value) {
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        hash.put(key, hashKey, value);
    }

    /**
     * 哈希获取数据
     *
     * @param key
     * @param hashKey
     * @return
     */
    public Object hmGet(String key, Object hashKey) {
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        return hash.get(key, hashKey);
    }

    /**
     * 列表添加
     *
     * @param k
     * @param v
     */
    public void lPush(String k, Object v) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        list.rightPush(k, v);
    }

    /**
     * 列表获取
     *
     * @param k
     * @param l
     * @param l1
     * @return
     */
    public List<Object> lRange(String k, long l, long l1) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        return list.range(k, l, l1);
    }

    /**
     * 集合添加
     *
     * @param key
     * @param value
     */
    public void add(String key, Object value) {
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        set.add(key, value);
    }

    /**
     * 集合获取
     *
     * @param key
     * @return
     */
    public Set<Object> setMembers(String key) {
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        return set.members(key);
    }

    /**
     * 有序集合添加
     *
     * @param key
     * @param value
     * @param source
     */
    public void zAdd(String key, Object value, double source) {
        ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        zset.add(key, value, source);
    }

    /**
     * 有序集合获取
     *
     * @param key
     * @param source
     * @param source1
     * @return
     */
    public Set<Object> rangeByScore(String key, double source, double source1) {
        ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        return zset.rangeByScore(key, source, source1);
    }
}
